package com.zzsy.carosshopprojects.view.keyboard;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import com.zzsy.carosshopprojects.R;

import androidx.annotation.NonNull;

/**
 * 输入金额键盘
 * Created by longtao.li on 2016/11/19.
 */
@SuppressLint("AppCompatCustomView")
public class AmountInputView extends TextView {
    //缓存输入的密码
    private StringBuilder inputSB;
    boolean isSelect = false;
    String def = "";

    public AmountInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inputSB = new StringBuilder();
        init();
    }

    private void init() {
        inputSB.append(def);
        setText(getResources().getString(R.string.rmb) + def.toString());
    }


    /**
     * 输入一个字符
     *
     * @param text
     */
    public synchronized void addText(String text) {
        if (inputSB.length() >= 7) {
            return;
        }
        if (".".equals(text)) {
            if ( isSelect ==false){
                inputSB.append(text);
                isSelect =true;
            }
        }else {
            inputSB.append(text);
        }
        String str = inputSB.toString();
        String cuttedStr = filter(str);

        inputSB.setLength(0);
        inputSB.append(cuttedStr);

        setText(getResources().getString(R.string.rmb) + inputSB.toString());
        invalidate();
    }

    @NonNull
    private String filter(String str) {
        String cutedStr = str;
        /* 删除字符串中的dot */
        // 只能输入一个小数点
        if (cutedStr.toString().contains(".")) {

        }
        // 判断小数点后只能输入两位
        if (cutedStr.toString().contains(".")) {
            if (cutedStr.length() - 1 - cutedStr.toString().indexOf(".") > 2) {
                cutedStr = (String) cutedStr.toString().subSequence(0, cutedStr.toString().indexOf(".") + 3);
            }
        } //如果第一个数字为0，第二个不为点，就不允许输入
        if (cutedStr.toString().startsWith("0") && cutedStr.toString().trim().length() > 1) {
            if (!cutedStr.toString().substring(1, 2).equals(".")) {
                cutedStr = (String) cutedStr.subSequence(0, 1);
            }
        } //如果第一个输入的为点，自动在前面加0 要不会闪退
        if (cutedStr.toString().startsWith(".")) {
            cutedStr = "0.";
        }

        return cutedStr;
    }

    /**
     * 删除最后一个
     */
    public void delLast() {
        if ((inputSB.toString()).equals(def))
            return;
        inputSB.deleteCharAt(inputSB.length() - 1);

        if( !inputSB.toString().contains(".")){
            isSelect = false;
        }
      /*  String temp = inputSB.toString();
        temp = filter(temp);
        inputSB.setLength(0);
        inputSB.append(temp);*/
        setText(getResources().getString(R.string.rmb) + inputSB.toString());
    }


    /**
     * 清空
     */
    public void clean() {
        inputSB.setLength(0);
        inputSB.append(def);
        setText(getResources().getString(R.string.rmb) + String.valueOf(inputSB));
    }

    /**
     * 返回输入内容
     **/
    public CharSequence getAmountText() {
        return inputSB.toString();
    }

}
